/**
 * Captain
 * © 2018 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 *
 * JAVA : 8
 * 文  件  名: AccessoryUtils.java
 * 创  建  人: QinYoubin (qin.youbin@sccaptain.com.cn)
 * 创建时间: 2018年1月23日 上午10:43:08
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.web.util;

import cn.hutool.core.io.watch.WatchMonitor;
import cn.hutool.core.io.watch.Watcher;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 附件操作工具
 *
 * @since 1.0
 * @version 1.0
 * @author QinYoubin (qin.youbin@sccaptain.com.cn)
 */
public class ZipUtils {

  private ZipUtils(){
  }

  public static void doCompress(String srcFile, String zipFile) throws IOException {
    doCompress(new File(srcFile), new File(zipFile));
  }
  /**
   * 文件压缩
   *
   * @param srcFile
   *          目录或者单个文件
   * @param zipFile
   *          压缩后的ZIP文件
   */
  public static void doCompress(File srcFile, File zipFile) throws IOException {
    ZipOutputStream out = null;
    try {
      out = new ZipOutputStream(new FileOutputStream(zipFile));
      doCompress(srcFile, out);
    } catch (Exception e) {
      throw e;
    } finally {
      out.close();// 记得关闭资源
    }
  }

  public static void doCompress(String filelName, ZipOutputStream out) throws IOException {
    doCompress(new File(filelName), out);
  }

  public static void doCompress(File file, ZipOutputStream out) throws IOException {
    doCompress(file, out, "");
  }

  public static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException {
    if (inFile.isDirectory()) {
      File[] files = inFile.listFiles();
      if (files != null && files.length > 0) {
        for (File file : files) {
          String name = inFile.getName();
          if (!"".equals(dir)) {
            name = dir + "/" + name;
          }
          ZipUtils.doCompress(file, out, name);
        }
      }
    } else {
      ZipUtils.doZip(inFile, out, dir);
    }
  }
  /**
   * 将文件保存到
   * @param out
   * @param dir
   * @param name
   * @throws IOException
   * @since 1.0
   * @version 1.0
   * @author QinYoubin (qin.youbin@sccaptain.com.cn)
   */
  public static boolean doCompress(String filelName, ZipOutputStream out, String dir,String name) throws IOException {
    File inFile =new File(filelName);
    if (inFile.isDirectory()||!inFile.exists()) {
      return false;
    } else {
      ZipUtils.doZip(inFile, out, dir,name);
      return true;
    }
  }

  public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException {
    String entryName = null;
    if (!"".equals(dir)) {
      entryName = dir + "/" + inFile.getName();
    } else {
      entryName = inFile.getName();
    }
    ZipEntry entry = new ZipEntry(entryName);
    out.putNextEntry(entry);

    int len = 0;
    byte[] buffer = new byte[1024];
    FileInputStream fis = new FileInputStream(inFile);
    while ((len = fis.read(buffer)) > 0) {
      out.write(buffer, 0, len);
      out.flush();
    }
    out.closeEntry();
    fis.close();
  }
  /**
   * 将文件打包成指定名字
   * @param inFile
   * @param out
   * @param dir
   * @param name
   * @throws IOException
   * @since 1.0
   * @version 1.0
   * @author QinYoubin (qin.youbin@sccaptain.com.cn)
   */
  public static void doZip(File inFile, ZipOutputStream out, String dir,String name) throws IOException {
    String entryName = null;
    if (!"".equals(dir)) {
      entryName = dir + "/" + name;
    } else {
      entryName = name;
    }
    ZipEntry entry = new ZipEntry(entryName);
    out.putNextEntry(entry);

    int len = 0;
    byte[] buffer = new byte[1024];
    FileInputStream fis = new FileInputStream(inFile);
    while ((len = fis.read(buffer)) > 0) {
      out.write(buffer, 0, len);
      out.flush();
    }
    out.closeEntry();
    fis.close();
  }
  /**
   * 将byte数组写入zip
   * @param buffer
   * @param out
   * @param dir
   * @param name
   * @throws IOException
   * @since 1.0
   * @version 1.0
   * @author QinYoubin (qin.youbin@sccaptain.com.cn)
   */
  public static void doZip(byte[] buffer,ZipOutputStream out, String dir,String name) throws IOException {
    String entryName = null;
    if (!"".equals(dir)) {
      entryName = dir + "/" + name;
    } else {
      entryName = name;
    }
    ZipEntry entry = new ZipEntry(entryName);
    out.putNextEntry(entry);
    out.write(buffer);
    out.closeEntry();
  }
  public static void main(String[] args) throws IOException {
//    ZipOutputStream out = new ZipOutputStream(new FileOutputStream("E:\\you\\test.zip"));
//    doZip(new File("E:\\you\\网址.txt"),out,"test1","test2.txt");
//    out.close();
    File file = new File("d:/4.库存物资清查登记表.xls");
    WatchMonitor monitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY);
    monitor.setWatcher(new Watcher() {
      @Override
      public void onCreate(WatchEvent<?> event, Path currentPath) {
        System.out.println("创建");
      }

      @Override
      public void onModify(WatchEvent<?> event, Path currentPath) {
        System.out.println("修改");
      }

      @Override
      public void onDelete(WatchEvent<?> event, Path currentPath) {

      }

      @Override
      public void onOverflow(WatchEvent<?> event, Path currentPath) {

      }
    });
    monitor.setMaxDepth(3);
//启动监听
    monitor.start();
  }
}
